﻿/*
 * C# managed ESL Suite
 * 
 * Version: MPL 1.1
 *
 * The contents of this file are subject to the Mozilla Public License Version
 * 1.1 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 * http://www.mozilla.org/MPL/
 *
 * Software distributed under the License is distributed on an "AS IS" basis,
 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
 * for the specific language governing rights and limitations under the
 * License.
 * 
 * Contributor: 
 * Diego Toro <dftoro@yahoo.com>
 */

using System;
using System.Diagnostics;
using System.Threading;
using Managed.Esl.Suite;

namespace BasicEvent
{
  class Program
  {
    static void Main(string[] args)
    {
      Trace.WriteLine("Starting...");

      //EventManager eveManager = new EventManager();
      //EventManager eveManager = new EventManager("127.0.0.1", 8021, "ClueCon", EESLMode.Inbound);
      //EventManager eveManager = new EventManager("127.0.0.1", 8021, "ClueCon", "event plain CHANNEL_CREATE");
      EventManager eveManager = new EventManager("127.0.0.1", 8022, null, EESLMode.SynOutbound);

      eveManager.OnESLSession += new ESLConnectionSession(eveManager_OnESLSession);

      ThreadPool.QueueUserWorkItem((o) =>
      {
        try
        {
          eveManager.Start();
        }
        catch (Exception ex)
        {
          Trace.WriteLine(ex);
        }
      });

      Console.ReadLine();

      eveManager.Stop();

      Trace.WriteLine("End");
    }

    static void eveManager_OnESLSession(object Sender, ESLSession session)
    {
      Trace.WriteLine("eveManager_OnESLSession");

      //session.OnEventReceive += (a, b) =>
      //{
      //  Trace.WriteLine(b);
      //};

      //session.OnEventChannelReceive += (a, b) =>
      //{
      //  Trace.WriteLine(b);
      //  Trace.WriteLine(b.EventName);
      //};


      session.PreAnswer();
      session["hello"] = "world!!!";
      session["continue_on_fail"] = "true";
      session["hangup_after_bridge"] = "false";

      //session.Sleep(1000);
      session.Answer();
      //session.Sleep(3000);


      //session.Playback("D:/fs/git/Debug/sounds/music/8000/ponce-preludio-in-e-major.wav");
      //session.PlayAndGetDigits(1, 3, 3, 3000, "#", "D:/fs/git/Debug/sounds/en/us/callie/ivr/8000/ivr-account_number.wav",
      //  "D:/fs/git/Debug/sounds/en/us/callie/ivr/8000/ivr-first_name_first.wav", "entrada", @"\d+");
      //Trace.WriteLine("VAR=" + session.GetVariable("entrada"));
      //session.Bridge("sofia/internal/1001%192.168.7.1");

      //Trace.WriteLine("VAR=" + session["hello"]);
      //session.Playback("D:/fs/git/Debug/sounds/en/us/callie/ivr/8000/ivr-first_name_first.wav");

      //session.Sleep(3000);

      //session.SetVariable("hello", "world!!!!");
      //session.Info.ToString();
      Trace.WriteLine("VAR=" + session.GetVariable("hello") + " Th: " + Thread.CurrentThread.ManagedThreadId);
      Trace.WriteLine("VAR=" + session.GetVariable("Caller-Caller-ID-Name") + " Th: " + Thread.CurrentThread.ManagedThreadId);
      //session.Hangup();

      //Trace.WriteLine(session.PreAnswer());
      //Trace.WriteLine(session.Sleep(3000));
      //Trace.WriteLine(session.Answer());
      //Trace.WriteLine(session.Sleep(3000));
      //Trace.WriteLine(session.Playback("D:/fs/git/Debug/sounds/music/8000/ponce-preludio-in-e-major.wav"));
      //Trace.WriteLine(session.Bridge("sofia/internal/1001%192.168.7.1"));

      //Trace.WriteLine(session.Info.ToString());
      Trace.WriteLine(session.Hangup());

      Trace.WriteLine("fin" + " Th: " + Thread.CurrentThread.ManagedThreadId);

      Trace.Flush();

    }


  }
}
